package com.sunyard.manage.controller;

import com.sunyard.dal.bo.PaginationBO;
import com.sunyard.dal.bo.task.*;
import com.sunyard.dal.dto.znsTask.AuditTaskDetailDTO;
import com.sunyard.dal.dto.znsTask.OrgRoleDTO;
import com.sunyard.dal.dto.znsTask.TaskZNSDTO;
import com.sunyard.manage.service.TaskService;
import com.sunyard.utils.Response.BaseResult;
import com.sunyard.utils.Response.GetResult;
import com.sunyard.utils.manage.DataGrid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author jimin.duan
 * @Description TODO
 * @createTime 2022年06月24日 15:04:00
 */
@RestController
@Api(tags = "TaskController", description = "任务管理")
@RequestMapping("/task")
public class TaskController {

    @Autowired
    private TaskService service;

    /***
     * 根据机构查询人员
     * */
    @PostMapping("/findPeopleByOrg")
    @ApiOperation("根据机构查询人员")
    public BaseResult findPeopleByOrg(@RequestBody OrgBO orgBO) {
        return service.findPeopleByOrg(orgBO);
    }

    /**
     * 根据机构返回锁具
     */
    @ApiOperation("根据机构查锁具")
    @PostMapping("/findLockByOP")
    public BaseResult findLockByOP(@RequestBody LockBO lock) {
        return service.findLockByOP(lock);

    }

    /**
     * 分页查询任务
     */
    @PostMapping("/find")
    @ApiOperation("查询任务")
    public BaseResult<DataGrid<TaskZNSDTO>> findTask(@RequestBody PaginationBO<TaskFindBO> taskFindBO, HttpServletRequest request) throws Exception {
        return service.findTask(taskFindBO,request);
    }

    /**
     * 根据id查询任务
     */
    @PostMapping("/findById")
    @ApiOperation("根据id查询任务")
    public BaseResult<TaskADDBO> findTaskById(@RequestBody TaskQueryBO taskQueryBO) {
        return service.findById(taskQueryBO.getTaskId());
    }

    /**
     * 添加任务
     */
    @PostMapping("/add")
    @ApiOperation("/添加任务")
    public BaseResult addTask(@RequestBody TaskADDBO taskADDBO, HttpServletRequest request) {
        try {
            return service.addTask(taskADDBO, request);
        } catch (Exception e) {
            e.printStackTrace();
            return GetResult.getResultFail(e.getMessage());
        }
    }

    /**
     * 删除任务
     */
    @ApiOperation("/删除任务")
    @PostMapping("/delete")
    public BaseResult deleteTask(@RequestBody TaskDeleteBO taskDelete, HttpServletRequest request) {
        return service.deleteTask(taskDelete, request);
    }
//    /**
//     * 分配审核任务
//     * */
//    @ApiOperation("/分配审核任务")
//    @PostMapping("/assignmentAuditTask")
//    public BaseResult assignmentAuditTask(@RequestBody TaskAuditBO taskAuditBO){
//        return service.assignmentAuditTask(taskAuditBO);
//    }

    /**
     * 审核任务
     */
    @ApiOperation("审核任务")
    @PostMapping("/auditTask")
    public BaseResult auditTask(@RequestBody TaskCopyBO taskCopyBO, HttpServletRequest request) {
        try {
            return service.auditTask(taskCopyBO, request);
        } catch (Exception e) {
            e.printStackTrace();
            return GetResult.getResultErr();
        }
    }

    /**
     * 查询当前登录人应该审核的任务列表
     */
    @ApiOperation("查询当前登录人审核的任务列表")
    @PostMapping("/taskListForLogin")
    public BaseResult<DataGrid<TaskZNSDTO>> taskListForLogin(@RequestBody PaginationBO<TaskListForLoginBO> taskListForLoginBO, HttpServletRequest request) throws Exception {
        return service.taskListForLogin(taskListForLoginBO, request);
    }

    /**
     * @Description: 任务审核导出
     * @Author: tsz
     */
    @ApiOperation("任务审核导出")
    @RequestMapping(value = "/taskReviewExport")
    @ResponseBody
    public void taskReviewExport(HttpServletResponse response, @RequestBody TaskListForLoginBO taskListForLoginBO, HttpServletRequest request) {
        service.taskReviewExport(response,taskListForLoginBO,request);
    }


    /**
     * 审核任务详情
     */
    @ApiOperation("审核任务详情")
    @PostMapping("/auditTaskDetail")
    public BaseResult<AuditTaskDetailDTO> auditTaskDetail(@RequestBody AuditTaskDetailBO auditTaskDetailBO) {
        return service.auditTaskDetail(auditTaskDetailBO);
    }

    /**
     * 任务下发  前端跟小程序 交互  记录下发状态
     */
    @ApiOperation("/任务下发")
    @PostMapping("/issued")
    public BaseResult issuedTask(@RequestBody TaskissuedBO taskissued, HttpServletRequest request) {
        return service.issuedTask(taskissued, request);
    }


    @ApiOperation("查询所有押运机构")
    @PostMapping("/findAllORG")
    public BaseResult findAllORG() {
        return service.findAllORG();
    }

    @ApiOperation("分页查询已下发任务")
    @PostMapping("/findTaskIssueds")
    public BaseResult<DataGrid<TaskZNSDTO>> findTaskIssued(@RequestBody PaginationBO<TaskFindBOForInssued> TaskFindBOForInssued, HttpServletRequest request) throws Exception {
        return service.findTaskIssued(TaskFindBOForInssued, request);
    }

    @ApiOperation("不分页查询下发任务")
    @PostMapping("/findTaskIssuedsNOpage")
    public BaseResult<List<TaskZNSDTO>> findTaskIssuedtwo(@RequestBody TaskFindBOForInssued taskFindBOForInssued) {
        return service.findTaskIssuedtwo(taskFindBOForInssued);
    }

    @ApiOperation("根据机构查尾箱")
    @PostMapping("/findBoxByOrg")
    public BaseResult findBoxByOrg(@RequestBody LockBO lock) {
        return service.findBoxByOrg(lock);

    }

    @ApiOperation("/根据尾箱查询锁具")
    @PostMapping("/findLockByBox")
    public BaseResult findLockByBox(@RequestBody BoxBO boxBo) {
        return service.findLockByBox(boxBo);

    }

    @ApiOperation("/远程开箱")
    @PostMapping("/openLock")
    public BaseResult openLock(@RequestBody OpenLockBO openLockBO) {
        return service.openLock(openLockBO);
    }

    @ApiOperation("根据任务id查询该条任务锁信息")
    @PostMapping("/findLockInfo")
    public BaseResult findLockInfo(@RequestBody FindLockInfoBo f) {
        return service.findLockInfo(f);
    }

    @PostMapping("/findOperatorByOrg")
    @ApiOperation("根据机构查询场景关联的人员列表")
    public BaseResult<List<OrgRoleDTO>> findOperatorByOrg(@RequestBody TaskSceOrgBO orgBO) {
        return service.findOperatorByOrg(orgBO);
    }

}
